#include <Precomp.h>
#include "defs.h"
#include "Application.h"
#include "ServerConnection.h"
#include "ClientListener.h"
#include "UIManager.h"
#include "BaseGameState.h"
#include "GameStateTeamAndShipSelect.h"
#include "packet.h"
#include "GameWorld.h"
#include "player.h"
#include "AVUIButton.h"
#include "AVUIRadioButton.h"
#include "AVUINavigationWindow.h"
#include "AVUITransitioningPage.h"

using namespace AVUI;

void CGameStateTeamAndShipSelect::Initialize(CNetrekClientListener* pListener)
{
    CBaseGameState::Initialize(pListener);


    FrameworkElement* pFE = CUIManager::LoadXamlScene(L"Media\\Xaml\\TeamSelect.xaml");
    m_pRoot = pFE;                                                      

    ((NavigationWindow*)Application::get_Current()->get_MainWindow())->Navigate(m_pRoot);

    if(m_pRoot != NULL)
    {
        RoutedEventArgs eventArgs(TransitioningPage::get_TransitionInEvent());
        m_pRoot->RaiseEvent(&eventArgs);
    }

    pFE->Release();
    pFE = NULL;


    m_flTimeSinceLastRequest = 0.0f;

    object_cast<Button>(m_pRoot->FindName(object_allocate<String>(L"FedTeam")))->add_Click(RoutedEventHandler::Create(this, &CGameStateTeamAndShipSelect::FedTeamClicked));
    object_cast<Button>(m_pRoot->FindName(object_allocate<String>(L"RomTeam")))->add_Click(RoutedEventHandler::Create(this, &CGameStateTeamAndShipSelect::RomTeamClicked));
    object_cast<Button>(m_pRoot->FindName(object_allocate<String>(L"KliTeam")))->add_Click(RoutedEventHandler::Create(this, &CGameStateTeamAndShipSelect::KliTeamClicked));
    object_cast<Button>(m_pRoot->FindName(object_allocate<String>(L"OriTeam")))->add_Click(RoutedEventHandler::Create(this, &CGameStateTeamAndShipSelect::OriTeamClicked));
}

void CGameStateTeamAndShipSelect::UpdateValidTeams()
{
    DWORD dwValidTeamMask = m_pListener->GetGameWorld()->GetGalaxyStatus()->GetValidTeamMask();

    // The !! is used here to convert to bool.

    EnableUIElement(L"FedTeam", !!(dwValidTeamMask & eTeamFlag_FED));
    EnableUIElement(L"RomTeam", !!(dwValidTeamMask & eTeamFlag_ROM));
    EnableUIElement(L"KliTeam", !!(dwValidTeamMask & eTeamFlag_KLI));
    EnableUIElement(L"OriTeam", !!(dwValidTeamMask & eTeamFlag_ORI));
}

void CGameStateTeamAndShipSelect::EnableUIElement(const WCHAR* wzName, bool isEnabled)
{
    if(isEnabled)
    {
        object_cast<UIElement>(m_pRoot->FindName(object_allocate<String>(wzName)))->set_IsEnabled(true);
    }
    else
    {
        object_cast<UIElement>(m_pRoot->FindName(object_allocate<String>(wzName)))->set_IsEnabled(false);
    }
}


void CGameStateTeamAndShipSelect::FrameStart()
{
    m_pListener->GetServerConnection()->UpdateWithPacketSink(this);


    UpdateValidTeams();

}

void CGameStateTeamAndShipSelect::Cleanup()
{
    Application::get_Current()->get_MainWindow()->set_Content(NULL);
}

bool CGameStateTeamAndShipSelect::ProcessPacket(EServerPacketType ePacketType, const packet_base* pPacket)
{
    if(ePacketType == EServerPacketType_TeamAndShipPickAccepted)
    {
        pickok_spacket* pPickPacket = (pickok_spacket*) pPacket;

        if(pPickPacket->state == 1)
        {
            TransitionMoveToNewState(m_pRoot, eNetrekClientState_InGame);
        }
        else
        {
            m_pListener->GetUIManager()->ShowOKDialog(L"Pick rejected.", DialogResultCallback(&CGameStateTeamAndShipSelect::PickRejectedOK, this));
        }

        return false;
    }
    else
    {
        m_pListener->GetGameWorld()->ProcessPacket(ePacketType, pPacket);
    }

    return true;
}

void CGameStateTeamAndShipSelect::TeamSelected(ETeamID eTeamID)
{
//    m_pListener->GetServerConnection()->SendTeamRequest(eTeamID, GetShipType());
      m_pListener->GetServerConnection()->SendTeamRequest(eTeamID, eShipType_Cruiser);
}

EShipType CGameStateTeamAndShipSelect::GetShipType()
{
    if(((RadioButton*)m_pRoot->FindName(object_allocate<String>(L"RefitScout")))->get_IsChecked())
    {
        return eShipType_Scout;
    }
    else if(((RadioButton*)m_pRoot->FindName(object_allocate<String>(L"RefitDestroyer")))->get_IsChecked())
    {
        return eShipType_Destroyer;
    }
    else if(((RadioButton*)m_pRoot->FindName(object_allocate<String>(L"RefitCruiser")))->get_IsChecked())
    {
        return eShipType_Cruiser;
    }
    else if(((RadioButton*)m_pRoot->FindName(object_allocate<String>(L"RefitBattleship")))->get_IsChecked())
    {
        return eShipType_Battleship;
    }
    else if(((RadioButton*)m_pRoot->FindName(object_allocate<String>(L"RefitAssault")))->get_IsChecked())
    {
        return eShipType_Assault;
    }
    else
    {
        return eShipType_Starbase;
    }

}


void CGameStateTeamAndShipSelect::FedTeamClicked(AVUI::Object*, AVUI::RoutedEventArgs*) { TeamSelected(eTeamID_FED); }
void CGameStateTeamAndShipSelect::RomTeamClicked(AVUI::Object*, AVUI::RoutedEventArgs*) { TeamSelected(eTeamID_ROM); }
void CGameStateTeamAndShipSelect::KliTeamClicked(AVUI::Object*, AVUI::RoutedEventArgs*) { TeamSelected(eTeamID_KLI); }
void CGameStateTeamAndShipSelect::OriTeamClicked(AVUI::Object*, AVUI::RoutedEventArgs*) { TeamSelected(eTeamID_ORI); }

